<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-10-08 16:09:54
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;

use App\Repositories\VmStorageRepository;
use Illuminate\Support\Facades\App;
use App\Http\Services\Service;
use App\Repositories\BatchStorageRepository;
use App\Repositories\StorageRepository;
use App\Repositories\MpcSummaryDetailRepository;
use Illuminate\Support\Facades\DB;

/**
 * Summary of StorageService
 * @property MpcSummaryDetailRepository $detail
 * @property BatchStorageRepository $batch
 * @property VmStorageRepository $vm
 * @author sunkaiyuan
 */
class StorageService extends Service
    {

    public function __construct(
        StorageRepository $Repository,
        MpcSummaryDetailRepository $detail,
        BatchStorageRepository $batch,
        VmStorageRepository $vm
    ) {
        $this->repository = $Repository;
        $this->detail     = $detail;
        $this->batch      = $batch;
        $this->vm         = $vm;
        }
    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function batch()
        {
        $data = $this->batch->condition(request())->getGrid();
        return $data;
        }
    public function allyarn()
        {
        return $this->vm->condition(request())->getGrid();
        }

    public function browser()
        {
        $request    = request();
        $ids        = [0];
        $repository = $this->getFilter("repository");
        if ($repository) {
            $repos = App::make("App\\Repositories\\{$repository}Repository");
            $ids   = $repos->whereStatus_id(0)->where("mpc_batch_id", ">", 0)->whereHas("delivery", function ($query)
                {
                $query->where("id", ">", 0);
                })->pluck("mpc_batch_id");
            }

        $data = $this->repository->setRelation(['order' => ['main']])->condition($request)
            ->where("available_quantity", ">", 0)
            ->whereNotIn("id", $ids)
            ->getGrid();
        // dd(sql());
        return $data;

        }
    public function planYarnlist()
        {
        foreach (request('filter') as $value) {
            foreach ($value as $key => $v)
                if ($key == 'sys_mpc_lot_defaultname') {
                    $this->repository->setQureySearch("sys_mpc_lot.name", $v['value'], $v['type'], $this->repository);
                    }
                elseif ($key == 'sys_mpc_location_name') {
                    $this->repository->setQureySearch("basedata_mpc_location.name", $v['value'], $v['type'], $this->repository);
                    }
                elseif ($key == 'available_quantity') {
                    $this->repository->setQureySearch("sys_mpc_summary_storage.available_quantity", $v['value'], $v['type'], $this->repository);
                    }
                else
                    $this->repository->where("sys_mpc_summary_storage.{$key}", $v);
            }
        // DB::enableQueryLog();
        $data = $this->repository->setRelation([])
            ->where("sys_mpc_summary_storage.color_id", "=", 0) //白纱
            ->where("basedata_mpc_location.department_id", "=", 33) //仓储库存，非门市部
            // ->condition(request())
            ->leftjoin("sys_mpc_lot", "sys_mpc_lot.id", "sys_mpc_summary_storage.lot_id")
            ->leftjoin("basedata_mpc_location", "basedata_mpc_location.id", "sys_mpc_summary_storage.location_id")
            ->select(
                DB::raw("group_concat(sys_mpc_summary_storage.id) as id"),
                DB::raw("group_concat(sys_mpc_summary_storage.remark) as remark"),
                DB::raw("sys_mpc_lot.name as sys_mpc_lot_defaultname"),
                DB::raw("max(sys_mpc_lot.id) as sys_mpc_lot_id"),
                DB::raw("basedata_mpc_location.name as sys_mpc_location_name"),
                DB::raw("SUM(available_quantity) AS available_quantity"),
                DB::raw("0 AS isrepair")
            )
            ->groupBy("sys_mpc_lot.name", "basedata_mpc_location.name")
            ->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }

    public function storageItems()
        {
        return $this->detail->condition()->getGrid();
        }
    }